//
//  GuideDiagnoseViewController.m
//  HarmonizePub
//
//  Created by Haven on 16/10/13.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "GuideDiagnoseViewController.h"
#import "GuideSymptom.h"
#import "Config.h"
#import "GuideDiagnose.h"
#import "MJExtension.h"
#import "IntelligentResultViewController.h"

@interface GuideDiagnoseViewController ()
@property (nonatomic, strong) GuideDiagnose *guideDiagnose;
@property (weak, nonatomic) IBOutlet UITextView *textView;

@property (weak, nonatomic) IBOutlet UIButton *yesBtn;

@property (weak, nonatomic) IBOutlet UIButton *noBtn;

@property (nonatomic, strong) NSString *isSelected;

@property (nonatomic, strong) NSString *diagnoseId;

@property (nonatomic, assign) int isEnd; // 是否结束

@property (nonatomic, strong) NSArray *dpLists; // 推荐科室

@property (nonatomic, copy) NSString *diseaseList; // 就诊建议

@end

@implementation GuideDiagnoseViewController
- (NSArray *)dpLists
{
    if (_dpLists == nil) {
        _dpLists = [[NSArray alloc] init];
    }
    return _dpLists;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isSelected = @"";
    self.diagnoseId = @"";
    self.title = [NSString stringWithFormat:@"%@自测", self.guideSymptom.symptomName];
    // 加载导诊列表
    [self loadGuideDiagnose];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];
    self.isSelected = @"";
    self.diagnoseId = @"";
    // 加载导诊列表
    [self loadGuideDiagnose];
}


// 加载导诊列表
- (void)loadGuideDiagnose
{
    NSDictionary *cookie = @{@"cookie" : [UserDefaultsTool stringForKey:XHToken]};
    NSDictionary *params = @{@"symptomId" : @(self.guideSymptom.id),
                             @"isSelected" : self.isSelected,
                             @"diagnoseId" : self.diagnoseId};
    [HTTPSTool POSTRequestWithPath: XHGetGuideDiagnose cookie: cookie params: params success:^(NSDictionary * _Nonnull JSON) {
        
        self.guideDiagnose = [GuideDiagnose mj_objectWithKeyValues: JSON[@"data"][@"guideDiagnose"]];
//        XHLog(@"JSON%@", JSON);
        if ([self.isSelected isEqualToString:@""] && [self.diagnoseId isEqualToString:@""]) {
//            XHLog(@"第一次");
            self.textView.text = self.guideDiagnose.diagnoseContent;
            self.diagnoseId = [NSString stringWithFormat:@"%d", self.guideDiagnose.id];
            self.isEnd = self.guideDiagnose.isEnd;
        }else{
//            XHLog(@"第n次");
            self.textView.text = self.guideDiagnose.diagnoseContent;
            self.diagnoseId = [NSString stringWithFormat:@"%d", self.guideDiagnose.id];;
            self.isEnd = self.guideDiagnose.isEnd;
        }
        
        if (self.guideDiagnose.isEnd) {
//            XHLog(@"JSON%@", JSON);
            IntelligentResultViewController *resultViewController = [[IntelligentResultViewController alloc] init];
            resultViewController.guideDiagnose = self.guideDiagnose;
            resultViewController.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController: resultViewController animated:YES];
        }

    }];
}

- (IBAction)yesBtn:(UIButton *)sender {
    self.isSelected = [NSString stringWithFormat:@"%ld", (long)sender.tag];
    [self loadGuideDiagnose];
}
- (IBAction)noBtn:(UIButton *)sender {
    self.isSelected = [NSString stringWithFormat:@"%ld", (long)sender.tag];
    [self loadGuideDiagnose];
    
}

@end
